<template>
  <el-dialog
    :title="isAdd ? '添加轮播图' : '编辑轮播图'"
    :visible.sync="isShow"
    center
    :before-close="cancel"
    :modal="false"
  >
    <el-form :model="form" :rules="rules" ref="form">
      <el-form-item prop="title" label="标题" :label-width="formLabelWidth">
        <el-input v-model="form.title" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="图片" :label-width="formLabelWidth">
        <el-upload
          action="#"
          list-type="picture-card"
          :on-preview="onPreview"
          :on-remove="onRemove"
          :file-list="fileList"
          :on-change="onChange"
          :limit="1"
          :on-exceed="handleExceed"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible" :modal-append-to-body="false">
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-switch
          v-model="form.status"
          active-color="#409EFF"
          inactive-color="#DCDFE6"
          :active-value="1"
          :inactive-value="2"
        >
        </el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="comfirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { bannerAdd, bannerEdit, bannerInfo } from "@/request/api";
import { mapActions } from "vuex";
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      fileList: [],
      form: {
        title: "", //轮播图名称
        img: "", //图片(文件)
        status: 1, //状态  1正常2禁用
      },
      formLabelWidth: "120px",
      rules: {
        title: [
          { required: true, message: "请输入轮播图名称", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    ...mapActions({
      getBanner: "banner/changeBannerAction",
    }),
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    onRemove(file, fileList) {
      console.log(file, fileList);
    },
    onPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    onChange(file, filelist) {
      console.log(file, filelist, "change");
      this.form.img = file.raw;
    },
    reset() {
      this.form = {
        title: "", //轮播图名称
        img: "", //图片(文件)
        status: 1, //状态  1正常2禁用
      };
      this.$refs.form.resetFields();
      this.fileList = [];
    },
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    lookup(id) {
      bannerInfo({
        id,
      }).then((res) => {
        console.log(res, "一条数据查询");
        if (res.code == 200) {
          this.form = res.list;
          this.form.id = id;
          this.fileList = [{ url: this.$imgUrl + res.list.img }];
        }
      });
    },
    comfirm() {
      this.$refs.form.validate((flag) => {
        if (flag) {
          console.log(this.form);
          if (this.isAdd) {
            // 轮播图添加
            bannerAdd(this.form).then((res) => {
              console.log(res, "轮播图添加");
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getBanner();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            // 轮播图编辑
            bannerEdit(this.form).then((res) => {
              console.log(res, "轮播图编辑");
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getBanner();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          alert("验证失败");
        }
      });
    },
  },
};
</script>

<style lang="less"scoped>
</style>
